package com.jy.modules.cims.component.creditRight;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.exception.InteractException;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleBeanUtils;
import com.jy.modules.cims.component.balance.SettleUtilsService;
import com.jy.modules.cims.component.common.service.TCMmCooperorgDealService;
import com.jy.modules.cims.component.deal.ICDeal;
import com.jy.modules.cims.component.sysAssist.BatchService;
import com.jy.modules.cims.data.common.dao.TCMmCreditDealDetailDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditDetailDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditPackageDao;
import com.jy.modules.cims.data.common.dao.TCMmQuotaCreditDetailDao;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDealDTO;
import com.jy.modules.cims.data.common.dto.TCMmCostAffiliationDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDealDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditPackageDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaCreditDetailDTO;
import com.jy.modules.cims.data.dao.CreditRightDao;
import com.jy.modules.cims.data.dao.DealDao;
import com.jy.modules.cims.data.dao.QuotaCreditDetailDao;
import com.jy.modules.cims.data.dao.QuotaDao;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.CreditDetailSecondDto;
import com.jy.modules.cims.data.dto.CreditRightDto;
import com.jy.modules.cims.data.dto.DealDetailDto;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.data.dto.QuotaCreditDetailDto;
import com.jy.modules.cims.data.dto.QuotaCreditRightDto;
import com.jy.modules.cims.data.dto.QuotaDetailUnlockParamDto;
import com.jy.modules.cims.data.dto.RedemptionCreditDetailDto;
import com.jy.modules.cims.data.dto.credit.CostAffiliationResultDto;
import com.jy.modules.cims.data.dto.credit.CreditRedeemSettleDto;
import com.jy.modules.cims.data.dto.credit.CreditTwoBuyMatchDto;
import com.jy.modules.cims.data.dto.credit.CreditTwoBuyRepayDto;
import com.jy.modules.cims.data.dto.credit.CreditTwoBuySettleDto;
import com.jy.modules.cims.data.dto.credit.CreditValueCalculateDto;
import com.jy.modules.cims.data.dto.credit.CreditValueCalculateResultDto;
import com.jy.modules.cims.data.dto.credit.PublicSettleParamDto;
import com.jy.modules.cims.data.dto.credit.PublicSettleResultDto;
import com.jy.modules.cims.data.dto.credit.SecondBuyCreditConfirmDto;
import com.jy.modules.cims.data.interact.api.request.RequestCreditRedemptionListDTO;
import com.jy.modules.cims.interact.ICInteract;
import com.jy.modules.cims.interact.IInteractAssist;
import com.jy.platform.api.org.SessionAPI;
import com.jy.platform.core.common.BaseDTO;

/**
 * 债权业务对象
 */
@Service("com.jy.modules.cims.component.creditRight.CCreditRightImpl")

public class CCreditRightImpl implements ICCreditRight {
	private static final Logger logger = LoggerFactory.getLogger(CCreditRightImpl.class);
	
	@Autowired
	private TCMmCreditPackageDao creditPackageDao;
	
	@Autowired
	private CreditRightDao crdao;

	@Autowired
	private TCMmCreditDealDetailDao creditDealDetailDao;

	@Autowired
	public QuotaDao quotaDao;

	@Autowired
	public QuotaCreditDetailDao quotaCreditDetailDao;

	@Autowired
	public TCMmQuotaCreditDetailDao tcmmQuotaCreditDetailDao;

	@Autowired
	private TCMmCreditDetailDao creditDetailDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CCInteractImpl")
	private ICInteract iCInteract;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CInteractAssist")
	public IInteractAssist iInteractAssist;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.sysAssist.BatchService")
	public BatchService batchService;
	
	
	@Autowired
    @Qualifier("com.jy.modules.cims.component.balance.SettleUtilsService")
    private SettleUtilsService settleUtilsService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCooperorgDealService")
	protected TCMmCooperorgDealService cooperorgDealService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.deal.CDealImpl")
	public ICDeal iCDeal;
	
	@Autowired
	public DealDao dealDao;
	
	@Autowired
	private SessionAPI sessionAPI;
	

	/**
	 * @author zhiqiang
	 * @description: 更新对象T_C_MM_QUOTA_CREDIT_DETAIL
	 * @date 2015-01-12 20:34:29
	 * @param paramMap
	 */
	@Override
	public void updateTCMmQuotaCreditDetailStatus(TCMmQuotaCreditDetailDTO qdto) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", qdto);
		crdao.updateTCMmQuotaCreditDetailStatus(paramMap);
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-1-23 下午3:00:05
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#updateTCMmQuotaCreditDetailStatusByIds(com.jy.modules.cims.data.common.dto.TCMmQuotaCreditDetailDTO)
	 */
	@Override
	public void updateTCMmQuotaCreditDetailStatusByTransferIds(QuotaCreditRightDto qdto) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", qdto);
		crdao.updateTCMmQuotaCreditDetailStatusByTransferIds(paramMap);
	}

	/**
	 * 更新赎回占用状态
	 */
	@Override
	public void updateCreditDetailStatus(RedemptionCreditDetailDto redemptionCreditDetailDto) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		redemptionCreditDetailDto.setTransferIds(redemptionCreditDetailDto.getTransferIds().replace(",", "','"));
		paramMap.put("dto", redemptionCreditDetailDto);
		crdao.updateCreditDetailStatus(paramMap);
	}
	/**
	 * 根据债权编号更新赎回占用状态
	 */
	@Override
	public void updateCreditDetailStatusByCreditCode(List<CreditDetailDto> creditDealList,Map<Object, Object> paramterMap) {
		
		DealDto dealDto = (DealDto)paramterMap.get("dealDto");
		
		StringBuffer transferIds = new StringBuffer();
		for (CreditDetailDto creditDetailDto : creditDealList) {
			transferIds.append(creditDetailDto.getCreditCode()+"','");
		}
		RedemptionCreditDetailDto redemptionCreditDetail =  new RedemptionCreditDetailDto();
		redemptionCreditDetail.setRedemptionStatus(CimsConstant.CREDIT_REDEMPTION_UNCK);// 赎回占用
		redemptionCreditDetail.setTransferIds(transferIds.substring(0, transferIds.length()-3));
		redemptionCreditDetail.setCooperorgCode(dealDto.getCooperorgCode());//机构编号
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", redemptionCreditDetail);
		crdao.updateCreditDetailStatusByCreditCode(paramMap);
	}

	/**
	 * 记录债权的挑选信息,先将债权的额度申请明细表的状态，然后插入到债权交易流水明细表、债权明细表
	 */
	@Override
	public void saveCreditDealDetail(QuotaCreditRightDto creditdto, TCMmCooperorgDealDTO dealdto) {

		String[] transferIds = creditdto.getTransferIds().split(",");
		for (int i = 0; i < transferIds.length; i++) {
			TCMmCreditDealDetailDTO cdddto = new TCMmCreditDealDetailDTO();
			cdddto.setDealStreamCode(dealdto.getDealCode());// 交易编号
			cdddto.setCooperorgCode(dealdto.getCooperorgCode());// 合作机构编号
			cdddto.setAgreementCode(creditdto.getArgeementCode());// 协议编号
			cdddto.setTransferId(transferIds[i]);// 转让编号
			cdddto.setDealSerial(1L);// 交易序号
			Map<String, Object> cdddtoparamMap = new HashMap<String, Object>();
			cdddtoparamMap.put("dto", cdddto);
			// 保存债权交易流水明细表
			creditDealDetailDao.insertTCMmCreditDealDetail(cdddtoparamMap);
		}

	}
	
	
	
	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-14 上午11:11:59
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#saveCreditDealDetailByQuotaCredit(com.jy.modules.cims.data.dto.QuotaCreditRightDto)
	 */
	@Override
	public void saveCreditDealDetailByQuotaCredit(QuotaCreditDetailDto qcreditdto) {
			TCMmCreditDealDetailDTO cdddto = new TCMmCreditDealDetailDTO();
			cdddto.setDealStreamCode(qcreditdto.getDealCode());// 交易编号
			cdddto.setCooperorgCode(qcreditdto.getCooperorgCode());// 合作机构编号
			cdddto.setTransferId(qcreditdto.getTransferId());// 转让编号
			cdddto.setDealSerial(1L);// 交易序号
			Map<String, Object> cdddtoparamMap = new HashMap<String, Object>();
			cdddtoparamMap.put("dto", cdddto);
			// 保存债权交易流水明细表
			creditDealDetailDao.insertTCMmCreditDealDetail(cdddtoparamMap);
	}

	/**
	 *  插入赎回债权交易明细
	 */
	@Override
	public void saveRedemptionCreditDealDetail(RedemptionCreditDetailDto redemptionCreditDetailDto,
			TCMmCooperorgDealDTO dealdto) {
		String[] transferIds = redemptionCreditDetailDto.getTransferIds().replace("'", "").split(",");
		String[] thisMakeCreditorValues = redemptionCreditDetailDto.getThisMakeCreditorValues().split(",");
		for (int i = 0; i < transferIds.length; i++) {
			TCMmCreditDealDetailDTO cdddto = new TCMmCreditDealDetailDTO();
			cdddto.setDealStreamCode(dealdto.getDealCode());// 交易编号
			cdddto.setCooperorgCode(dealdto.getCooperorgCode());// 合作机构编号
			cdddto.setAgreementCode(redemptionCreditDetailDto.getAgreementCode());// 协议编号
			cdddto.setTransferId(transferIds[i]);// 转让编号
			cdddto.setDealSerial(1L);// 交易序号
			cdddto.setThisMakeCreditorValue(new BigDecimal(thisMakeCreditorValues[i]));//赎回债权价值
			Map<String, Object> cdddtoparamMap = new HashMap<String, Object>();
			cdddtoparamMap.put("dto", cdddto);
			// 保存债权交易流水明细表
			creditDealDetailDao.insertTCMmCreditDealDetail(cdddtoparamMap);
		}
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("dto", dealdto);
			crdao.updateCreditDealDetailCautionMoney(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 插入赎回债权交易明细
	 */
	@Override
	public void saveRedemptionCreditDealDetail(List<CreditDetailDto> creditDealList, DealDto dealDto) {
		
		List<TCMmCreditDetailDTO> creditDetailList  =  new ArrayList<TCMmCreditDetailDTO>();
		Date redeemDate = null;
		for (CreditDetailDto creditDetailDto : creditDealList) {
			TCMmCreditDetailDTO dto = new TCMmCreditDetailDTO();
			dto.setCooperorgCode(dealDto.getCooperorgCode());
			dto.setCreditCode(creditDetailDto.getCreditCode());
			dto.setPackageNum(dealDto.getCreditPackageName());
			dto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WIN);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("dto", dto);
			List<TCMmCreditDetailDTO> list = creditDetailDao.searchTCMmCreditDetail(paramMap);
			if(list.size()==1){
				dto = list.get(0);
			}else if(list.size()==0){
				throw new CimsException(creditDetailDto.getCreditCode()+"：债权编号不存在，不能做赎回交易。");
			}else if(list.size()>1){
				throw new CimsException(creditDetailDto.getCreditCode()+"：债权编号有重复，请使用主动赎回。");
			}
			if(redeemDate == null){
				redeemDate = creditDetailDto.getRedeemDate();
			}
			TCMmCreditDetailDTO creditDetail = new TCMmCreditDetailDTO();
			creditDetail.setTransferId(dto.getTransferId());
			creditDetail.setCreditCode(dto.getCreditCode());
			creditDetail.setRedeemDate(creditDetailDto.getRedeemDate());
			creditDetail.setThisRepaySum(dto.getThisRepaySum());
			creditDetail.setMakeCreditorValue(creditDetailDto.getDealMakeCreditorValue());// 赎回价格
			creditDetailList.add(creditDetail);
			
		}
		
		creditDetailList = this.getCreditDetailRedemptionListByInterface(creditDetailList, redeemDate);
		if(creditDetailList==null){
			throw new InteractException(CimsExceptionConstant.INTERACT_SERVICE_FAIL);
		}
		for (TCMmCreditDetailDTO tcMmCreditDetailDTO : creditDetailList) {
			TCMmCreditDealDetailDTO cdddto = new TCMmCreditDealDetailDTO();
			cdddto.setDealStreamCode(dealDto.getDealCode());// 交易编号
			cdddto.setCooperorgCode(dealDto.getCooperorgCode());// 合作机构编号
			cdddto.setAgreementCode(dealDto.getAgreementCode());// 协议编号
			cdddto.setTransferId(tcMmCreditDetailDTO.getTransferId());// 转让编号
			cdddto.setMakeCreditorValue(tcMmCreditDetailDTO.getMakeCreditorValue());// 赎回价格
			cdddto.setThisMakeCreditorValue(tcMmCreditDetailDTO.getThisMakeCreditorValue());//赎回债权价值
			cdddto.setThisCreditorValue(tcMmCreditDetailDTO.getThisMakeCreditorValue());//赎回日期的债权价值
			cdddto.setRedeemDate(tcMmCreditDetailDTO.getRedeemDate());//赎回日期
			cdddto.setDealSerial(1L);// 交易序号
			Map<String, Object> cdddtoparamMap = new HashMap<String, Object>();
			cdddtoparamMap.put("dto", cdddto);
			// 保存债权交易流水明细表
			creditDealDetailDao.insertTCMmCreditDealDetail(cdddtoparamMap);
		}
		
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("dto", dealDto);
			crdao.updateCreditDealDetailCautionMoney(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 选择赎回债权，转换指定日期的债权价值
	 * 
	 * @param creditDetailList
	 * @param redeemDate
	 * @return
	 */
	public List<TCMmCreditDetailDTO> getCreditDetailRedemptionListByInterface(List<TCMmCreditDetailDTO> creditDetailList, Date redeemDate) {

		String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
		iInteractAssist.registerDealIrtService(busiNoForInteract);

		CreditValueCalculateDto creditValueCalculateDto = new CreditValueCalculateDto();
		List<CreditValueCalculateDto.CreditValueCalculateBatch> batchIdList = new ArrayList<CreditValueCalculateDto.CreditValueCalculateBatch>();
		/* 保存执行上下文 */
		for (TCMmCreditDetailDTO creditDetail : creditDetailList) {
			CreditValueCalculateDto.CreditValueCalculateBatch cc = new CreditValueCalculateDto.CreditValueCalculateBatch();
			cc.setTransferId(creditDetail.getTransferId());
			cc.setCreidtId(creditDetail.getCreditCode());
			cc.setCreditDate(redeemDate);
			cc.setEachRepaymentValue(creditDetail.getThisRepaySum());
			batchIdList.add(cc);
		}
		creditValueCalculateDto.setBatchIdList(batchIdList);
		creditValueCalculateDto.setDealCode(busiNoForInteract);
		logger.info("赎回调用4028："+JSON.toJSONString(creditValueCalculateDto));
		List<CreditValueCalculateResultDto> rslist = iCInteract.getCreditValue(creditValueCalculateDto);
		logger.info("赎回调用4028返回结果："+JSON.toJSONString(rslist));
		if (rslist == null) {
			return null;
		}
		Map<String, CreditValueCalculateResultDto> map = new HashMap<String, CreditValueCalculateResultDto>();
		for (CreditValueCalculateResultDto creditValueCalculateResultDto : rslist) {
			map.put(creditValueCalculateResultDto.getTransferId(), creditValueCalculateResultDto);
		}
		List<TCMmCreditDetailDTO> resultlist = new ArrayList<TCMmCreditDetailDTO>();
		for (TCMmCreditDetailDTO tcMmCreditDetailDTO : creditDetailList) {
			CreditValueCalculateResultDto creditValueCalculateResultDto = map.get(tcMmCreditDetailDTO.getTransferId());
			tcMmCreditDetailDTO.setThisMakeCreditorValue(creditValueCalculateResultDto.getCreditValue());
			resultlist.add(tcMmCreditDetailDTO);
		}
		return resultlist;
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @throws Exception 
	 * @date 2015-2-2 下午4:35:37
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#saveCreditDetail(com.jy.modules.cims.data.dto.QuotaCreditRightDto,
	 *      com.jy.modules.cims.data.common.dto.TCMmCooperorgDealDTO)
	 */
	@Override
	public QuotaDetailUnlockParamDto saveCreditDetail(QuotaCreditRightDto creditdto, TCMmCooperorgDealDTO dealdto) throws Exception {
		String[] transferIds = creditdto.getTransferIds().split(",");
		String[] thisMakeCreditorValues = creditdto.getThisMakeCreditorValues().split(",");
		String[] ids = creditdto.getIds().split(",");

		List<QuotaDetailUnlockParamDto.CreditRightUnlockInfo> creditRightUnlockInfoList = new ArrayList<QuotaDetailUnlockParamDto.CreditRightUnlockInfo>();

		for (int i = 0; i < transferIds.length; i++) {
			QuotaCreditDetailDto qcreditdto = quotaDao.findTCMmQuotaCreditDetailByPrimaryKey(String.valueOf(ids[i]));
			TCMmCreditDetailDTO cddto = new TCMmCreditDetailDTO();
				SimpleBeanUtils.copyBean2Bean(cddto, qcreditdto);
				if (new BigDecimal(thisMakeCreditorValues[i]).compareTo(cddto.getThisMakeCreditorValue()) != 0) {
					cddto.setThisMakeCreditorValue(new BigDecimal(thisMakeCreditorValues[i]));
					QuotaDetailUnlockParamDto.CreditRightUnlockInfo creditRightUnlockInfo = new QuotaDetailUnlockParamDto.CreditRightUnlockInfo();
					creditRightUnlockInfo.setCreditId(qcreditdto.getCreditCode());// 债权编号
					creditRightUnlockInfo.setTransferId(qcreditdto.getTransferId());// 转让编号
					// 释放百分比
					BigDecimal repaySumPercent = (qcreditdto.getThisMakeCreditorValue().subtract(new BigDecimal(thisMakeCreditorValues[i]))).divide(qcreditdto.getThisMakeCreditorValue(),6,BigDecimal.ROUND_HALF_UP);
//					creditRightUnlockInfo.setEachRepaymentValue(qcreditdto.getThisRepaySum().multiply(repaySumPercent));// //本次释放对应月还金额(本期还款金额)
					BigDecimal freeValue = qcreditdto.getThisMakeCreditorValue().subtract(new BigDecimal(thisMakeCreditorValues[i]));
					if(freeValue.compareTo(new BigDecimal("0"))<0){
						throw new Exception("转让金额不能大于标的日债权价值！");
					}
					creditRightUnlockInfo.setFreeValue(freeValue);// 本次释放债权价值（本次转让债权价值）
					//部分释放
					creditRightUnlockInfo.setIsWhole(CimsConstant.CREDIT_NOWHOLE);
					//标的日期（如果标的日期为当天，日期为空）
					creditRightUnlockInfo.setDeliveryDate(qcreditdto.getCreditReferDate());
					creditRightUnlockInfoList.add(creditRightUnlockInfo);
					// 更改月还金额
					cddto.setThisRepaySum(qcreditdto.getThisRepaySum().subtract(qcreditdto.getThisRepaySum().multiply(repaySumPercent)));
					cddto.setThisCreditorValue(new BigDecimal(thisMakeCreditorValues[i]));
				}
			
			Map<String, Object> cddtoparamMap = new HashMap<String, Object>();
			cddto.setId(null);
			cddto.setContractCode(dealdto.getContractCode());
			cddto.setCooperorgCode(dealdto.getCooperorgCode());
			cddto.setCreditSource(CimsConstant.CREDIT_SOURCE_CRE);//债权来源:01-债权池 
			cddto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WAY);//债权状态:01-在途
			cddto.setCreateTime(new Timestamp(new Date().getTime()));
			cddto.setCreditorValueUpdateDate(new java.util.Date());
			cddto.setProductType(creditdto.getProductType());//产品类型
			cddto.setRepayType(creditdto.getRepayType());//还款方式
			cddto.setPackageNum(creditdto.getPackageNum());//债权包
			
			if(null != creditdto.getPlanRedeemDate()){
				cddto.setPlanRedeemDate(creditdto.getPlanRedeemDate());//计划赎回日期
			}
			
			cddtoparamMap.put("dto", cddto);
			creditDetailDao.insertTCMmCreditDetail(cddtoparamMap);

		}
		if (creditRightUnlockInfoList.size() > 0) {
			QuotaDetailUnlockParamDto paramDto = new QuotaDetailUnlockParamDto();
			paramDto.setQuotaType(CimsConstant.QUOTATYPE_CR);
			paramDto.setCreditRightUnlockList(creditRightUnlockInfoList);
			return paramDto;
		} else {
			return null;
		}
	}
	
	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-14 上午11:51:43
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#saveCreditDetailByQuotaCredit(com.jy.modules.cims.data.dto.QuotaCreditDetailDto)
	 */
	@Override
	public void saveCreditDetailByQuotaCredit(QuotaCreditDetailDto quotaCreditDetailDto) throws Exception {
		Map<String, Object> cddtoparamMap = new HashMap<String, Object>();
		TCMmCreditDetailDTO cddto = new TCMmCreditDetailDTO();
		SimpleBeanUtils.copyBean2Bean(cddto, quotaCreditDetailDto);
		cddto.setId(null);
		cddto.setCreditSource(CimsConstant.CREDIT_SOURCE_CRE);// 债权来源:01-债权池
		cddto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WAY);// 债权状态:01-在途
		cddto.setCreateTime(new Timestamp(new Date().getTime()));
		cddto.setCreditorValueUpdateDate(new java.util.Date());
		cddto.setDeliverDate(quotaCreditDetailDto.getCreditReferDate());//交割日期=标的日期
		
		TCMmCreditPackageDTO creditPackage = new TCMmCreditPackageDTO();
		creditPackage.setCooperorgCode(quotaCreditDetailDto.getCooperorgCode());
		creditPackage.setContractCode(quotaCreditDetailDto.getContractCode());
		creditPackage.setCreditPackageName(quotaCreditDetailDto.getPackageNum());
		List<TCMmCreditPackageDTO> creditPackageList = creditPackageDao.searchTCMmCreditPackage(SearchParamUtil.getSearchParam(creditPackage));
		
		if(CollectionsUtils.isNotEmpty(creditPackageList)){
			cddto.setPlanRedeemDate(creditPackageList.get(0).getPlanRedeemDate());
		}
		cddtoparamMap.put("dto", cddto);
		
		creditDetailDao.insertTCMmCreditDetail(cddtoparamMap);
		
		creditPackage = null;
		cddtoparamMap = null;
		cddto = null;
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-1-23 下午3:41:36
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#deleteTCMmCreditDealDetailByTransferIds(com.jy.platform.core.common.BaseDTO,
	 *      java.lang.String)
	 */
	@Override
	public void deleteTCMmCreditDealDetailByTransferIds(BaseDTO baseDto, String transferIds,String dealCode) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("transferIds", transferIds);
		paramMap.put("dealCode", dealCode);
		crdao.deleteTCMmCreditDealDetailByTransferIds(paramMap);
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-1-23 下午4:14:17
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#deleteTCMmCreditDetailByTransferIds(com.jy.platform.core.common.BaseDTO,
	 *      java.lang.String)
	 */
	@Override
	public void deleteTCMmCreditDetailByTransferIds(BaseDTO baseDto, String transferIds) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("transferIds", transferIds);
		crdao.deleteTCMmCreditDetailByTransferIds(paramMap);
	}

	/**
	 * 债权业务对象
	 */
	public CCreditRightImpl() {
	}

	/**
	 * 保存债权信息
	 * 
	 * @param dto
	 * @return
	 */
	public void saveInfo(CreditRightDto dto) {

	}

	/**
	 * 交易明细对象转成债权对象
	 * 
	 * @param detail
	 * @return
	 */
	public CreditRightDto dealDetailToCreditRight(DealDetailDto detail) {
		return null;
	}

	/**
	 * 债权对象转成交易明细对象
	 * 
	 * @param dto
	 * @return
	 */
	public DealDetailDto creditRightToDealDetail(CreditRightDto dto) {
		return null;
	}

	/**
	 * 删除债权信息
	 */
	public void deleteInfo() {

	}

	/**
	 * @description 根据额度申请查询获取额度明细
	 * @param dto
	 * @return
	 */
	public List<QuotaCreditDetailDto> getQuotaCreditDetailByQuotaCreditDetail(QuotaCreditDetailDto dto) {
		return quotaCreditDetailDao.searchQuotaCreditDetailByQuotaCredit(SearchParamUtil.getSearchParam(dto));
	}
	/**
	 * @author lizhiqiang
	 * @date 2015-4-18 下午2:21:20
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#insertCreditDealDetailByBatch(java.util.List)
	 */
	@Override
	public void insertCreditDealDetailByBatch(List<TCMmCreditDealDetailDTO> dlist) {
		crdao.insertCreditDealDetailByBatch(dlist);
	}

	@Override
	public void insertCreditDetailByBatch(List<TCMmCreditDetailDTO> dlist) {
		crdao.insertCreditDetailByBatch(dlist);
		
	}
	
	/**
	 * @description 批量修改额度债权明细
	 * @param paramMap
	 * @return
	 */
	public int updateQuotaCreditDetailDealStatus(Map<String, Object> paramMap){
		return quotaCreditDetailDao.updateQuotaCreditDetailDealStatus(paramMap);
	}

	/**
	 * 
	 * @author lizhiqiang
	 * @date 2015-8-26 下午6:09:19
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#getSumThisMakeCreditValueByDealCode(com.jy.modules.cims.data.dto.DealDto)
	 */
	public BigDecimal getSumThisMakeCreditValueByDealCode(DealDto dealdto) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dealdto);
		return crdao.getSumThisMakeCreditValueByDealCode(paramMap);
	}
	
	/**
	 * 查询二次购买需要确认的债权列表
	 * @Title getSecondBuyCreditConfirm
	 * @Description TODO
	 * @param dealDto
	 * @return 
	 * List<SecondBuyCreditConfirmDto> 
	 * @author lizhiqiang
	 * @date 2015-9-23-上午10:06:38
	 * @update 
	 * @throws
	 */
	public List<SecondBuyCreditConfirmDto> getSecondBuyCreditConfirm(DealDto dealDto){
		TCMmCreditDealDetailDTO dto = new TCMmCreditDealDetailDTO();
		dto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WAY);
		dto.setDealStreamCode(dealDto.getDealCode());
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		return crdao.getSecondBuyCreditConfirm(paramMap);
	}
	/**
	 * @Description 判断是否是受让债权
	 * @param creditCode 债权编号
	 * @param creditDetailDTO 受让债权
	 * @return true:是受让债权； false:不是受让债权
	 * @author shengzhoutao
	 * @date 2015-9-22 
	 */
	public boolean isSRCreditRight(final String creditCode, TCMmCreditDetailDTO creditDetailDTO){
		TCMmCreditDetailDTO creditDetail = new TCMmCreditDetailDTO();
		creditDetail.setCreditCode(creditCode);
		creditDetail.setCreditSource(CimsConstant.CREDIT_SOURCE_REC);//来自债权受让
		List<TCMmCreditDetailDTO> creditDetailList = creditDetailDao.searchTCMmCreditDetail(SearchParamUtil.getSearchParam(creditDetail));
		creditDetail = null;
		if(CollectionsUtils.isNotEmpty(creditDetailList)){
			return true;
		}
		
		return false;
	}

	/**
	 * 转让赎回结算
	 * @author lizhiqiang
	 * @throws Exception 
	 * @date 2016-4-18 上午11:25:08
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#doRedempSettleDeal(java.lang.String)
	 */
	@Override
	public String doRedempSettleDeal(DealDto dealdto) {
		//1、取机构编号
		List<String> strlist = new ArrayList<String>();
		strlist.add(dealdto.getDealCode());
		/*
		 * 》》》》》》》贷款转出结算上线以后，以下四行代码要注释掉
		 */
		List<DealDto> checklist = dealDao.getCooperorgDealByDealCodeInTwoBuy(strlist);
		if(checklist!=null&&checklist.size()>=1){
			return dealdto.getDealCode()+":属于贷款转出的二次购买债权，暂不支持线上结算!";
		}
		
		List<DealDto> deallist = dealDao.getCooperorgDealByDealCodeList(strlist);
		if(deallist.size()>0){
			dealdto.setCooperorgCode(deallist.get(0).getCooperorgCode());
		}
		//2、生成结算编号
		String applyCode = settleUtilsService.autoMakeSettleApplyCode(dealdto.getDealCode());//结算编号
		//3、校验费用归属
		TCMmCostAffiliationDTO cadto = new TCMmCostAffiliationDTO();
		cadto.setCooperorgCode(dealdto.getCooperorgCode());//机构编号
		cadto.setBusinessScenarios(CimsConstant.DEAL_TYPE_CREDIT_REDEEM);//交易类型
		CostAffiliationResultDto costAffiliationResultDto = settleUtilsService.checkCost(cadto);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dealdto);
		List<CreditDetailDto> cdlist = crdao.getDealDetailListByDealcode(paramMap);
		if(cdlist.size()==0){
			//没有待结算的就处理被驳回的
			dealdto.setCostState(CimsConstant.COST_STATUS_BH);
			paramMap.clear();
			paramMap.put("dto", dealdto);
			cdlist = crdao.getDealDetailListByDealcode(paramMap);
			if(cdlist.size()==0){
				return dealdto.getDealCode()+":没有可结算的债权!";
			}
		}
		PublicSettleParamDto publicSettleParamDto = new PublicSettleParamDto();
		List<CreditRedeemSettleDto> listRedeemSettle = new ArrayList<CreditRedeemSettleDto>();
		for (CreditDetailDto creditDetailDto : cdlist) {
			CreditRedeemSettleDto creditRedeemSettleDto = new CreditRedeemSettleDto();
			creditRedeemSettleDto.setTransferId(creditDetailDto.getTransferId());//债权转让编号
			creditRedeemSettleDto.setDealCode(dealdto.getDealCode());//交易流水号
			creditRedeemSettleDto.setRedeemValue(creditDetailDto.getDealMakeCreditorValue());//赎回价格
			creditRedeemSettleDto.setServiceCharge(BigDecimal.ZERO);//服务费
			if(costAffiliationResultDto.isMarginFee()){
				creditRedeemSettleDto.setCashDeposit(creditDetailDto.getCautionMoney());//保证金
			}else{
				creditRedeemSettleDto.setCashDeposit(BigDecimal.ZERO);//保证金
			}
			creditRedeemSettleDto.setGuaranteeFee(BigDecimal.ZERO);//担保费
			listRedeemSettle.add(creditRedeemSettleDto);
		}
		publicSettleParamDto.setListRedeemSettle(listRedeemSettle);//结算债权列表
		publicSettleParamDto.setDealCode(dealdto.getDealCode());//交易流水号
		publicSettleParamDto.setApplyCode(applyCode);//结算流水号
		publicSettleParamDto.setCCode(dealdto.getCooperorgCode());//合作机构
		publicSettleParamDto.setApplyName(sessionAPI.getCurrentUserInfo().getUserName());//结算人申请人
		try {
			
			PublicSettleResultDto pr = iCInteract.sendZrCreditRedeemSettle(publicSettleParamDto);
			if(pr.isSuccess()&&pr.getRetCode().equals("0000")){
				HashMap<Object , Object> stateparamMap = new HashMap<Object , Object>();
				dealdto.setCostState(CimsConstant.COST_STATUS_SQ);//
				dealdto.setApplyCode(applyCode);
				stateparamMap.put("dealdto", dealdto);
				batchService.batchInvoke(CCreditRightImpl.class, "updateCreditDealDetailCostState", cdlist, stateparamMap, 500);
				return dealdto.getDealCode()+":发送结算申请成功";
			}else{
				return dealdto.getDealCode()+":发送结算申请失败,"+pr.getRetMsg();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return dealdto.getDealCode()+":发送结算申请失败,"+e.getMessage();
		}
	}
	/**
	 * 赎回后更新结算状态
	 * @author lizhiqiang
	 * @date 2016-4-19 下午2:57:44
	 * @see com.jy.modules.cims.component.creditRight.ICCreditRight#updateCreditDealDetailCostState(java.util.List, java.util.HashMap)
	 */
	public void updateCreditDealDetailCostState(List<CreditDetailDto> cdlist,Map<Object , Object> paramMap){
		DealDto dealdto = (DealDto) paramMap.get("dealdto");
		StringBuffer ids = new StringBuffer();
		for (CreditDetailDto creditDetailDto : cdlist) {
			ids.append(creditDetailDto.getTransferId()).append("','");
		}
		HashMap<String , Object> stateparamMap = new HashMap<String , Object>();
		stateparamMap.put("dto", dealdto);
		stateparamMap.put("ids", "'"+ids.toString().substring(0, ids.length()-2));
		crdao.updateCreditDealDetailCostState(stateparamMap);//更新交易流水明细结算状态
//		crdao.updateCooperorgDealCostState(stateparamMap);//更新交易流水结算状态
	}

	@Override
	public List<CreditTwoBuyRepayDto> getCreditTwoBuyRepayList(DealDto dealDto) {
		return crdao.getCreditTwoBuyRepayList(SearchParamUtil.getSearchParam(dealDto));
	}

	@Override
	public List<CreditTwoBuySettleDto> getCreditTwoBuySettleList(DealDto dealDto) {
		return crdao.getCreditTwoBuySettleList(SearchParamUtil.getSearchParam(dealDto));
	}

	@Override
	public List<CreditTwoBuyMatchDto> getCreditTwoBuyMatchList(DealDto dealDto) {
		return crdao.getCreditTwoBuyMatchList(SearchParamUtil.getSearchParam(dealDto));
	}
	
	@Override
	public void updateCreditDetailCreditorStatus(DealDto dealDto){
		crdao.updateCreditDetailCreditorStatus(SearchParamUtil.getSearchParam(dealDto));
	}
	/**
	 * 债权赎回接口-对外系统
	 * @author zhangmi
	 * @date  2017年1月11日
	 * @param creditList
	 * @throws Exception
	 */
	public String doReceiveCreditRedemption(List<RequestCreditRedemptionListDTO> creditList) throws Exception{
		
		List<TCMmCreditDetailDTO> creditDetailList  =  new ArrayList<TCMmCreditDetailDTO>();
		Date redeemDate = null;
     	String dealCode = this.doGetDealCode();
     	TCMmCreditDetailDTO creditDetail = null;
     	TCMmCreditDetailDTO searchDto = null;
		for (RequestCreditRedemptionListDTO requestCreditDto : creditList) {
			TCMmCreditDetailDTO tcmmCreditDetailDto = null;
			searchDto = new TCMmCreditDetailDTO();
			searchDto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WIN);
			searchDto.setTransferId(requestCreditDto.getTransferId());
			searchDto.setCreditCode(requestCreditDto.getCreditCode());
			List<TCMmCreditDetailDTO> list = creditDetailDao.searchTCMmCreditDetail(SearchParamUtil.getSearchParam(searchDto));
			if(CollectionsUtils.isEmpty(list)){
				throw new CimsException(requestCreditDto.getTransferId()+"：债权转让编号不存在，不能做赎回交易。");
			}else{
				if(list.size()>1){
					throw new CimsException(requestCreditDto.getTransferId()+"：债权转让编号有重复，请使用主动赎回。");
				}else if(list.size()==1){
					tcmmCreditDetailDto = list.get(0);
				}
			}
			if(redeemDate == null){
				redeemDate = requestCreditDto.getRedeemDate();
			}
			creditDetail = new TCMmCreditDetailDTO();
			

			creditDetail.setCooperorgCode(tcmmCreditDetailDto.getCooperorgCode());// 合作机构编号
			creditDetail.setAgreementCode(tcmmCreditDetailDto.getAgreementCode());// 协议编号
			creditDetail.setContractCode(tcmmCreditDetailDto.getContractCode());//合约
			creditDetail.setPackageNum(tcmmCreditDetailDto.getPackageNum());//包号
			creditDetail.setTransferId(tcmmCreditDetailDto.getTransferId());
			creditDetail.setCreditCode(tcmmCreditDetailDto.getCreditCode());
			creditDetail.setRedeemDate(redeemDate);
			creditDetail.setThisRepaySum(tcmmCreditDetailDto.getThisRepaySum());
			creditDetail.setMakeCreditorValue(requestCreditDto.getMakeCreditorValue());// 赎回价格
			creditDetailList.add(creditDetail);
			
		}
		
		creditDetailList = this.getCreditDetailRedemptionListByInterface(creditDetailList, redeemDate);
		if(creditDetailList==null){
			throw new InteractException(CimsExceptionConstant.INTERACT_SERVICE_FAIL+"，债权赎回操作！");
		}
		TCMmCreditDealDetailDTO cdddto = null;
		for (TCMmCreditDetailDTO tcMmCreditDetailDTO : creditDetailList) {
			cdddto = new TCMmCreditDealDetailDTO();
			cdddto.setDealStreamCode(dealCode);// 交易编号
			cdddto.setCooperorgCode(tcMmCreditDetailDTO.getCooperorgCode());// 合作机构编号
			cdddto.setAgreementCode(tcMmCreditDetailDTO.getAgreementCode());// 协议编号
			cdddto.setTransferId(tcMmCreditDetailDTO.getTransferId());// 转让编号
			cdddto.setCreditorId(tcMmCreditDetailDTO.getCreditCode());//债权编号
			cdddto.setCreditorStatus(CimsConstant.CREDIT_STATUS_RED);//赎回
			cdddto.setMakeCreditorValue(tcMmCreditDetailDTO.getMakeCreditorValue());// 赎回价格
			cdddto.setThisMakeCreditorValue(tcMmCreditDetailDTO.getThisMakeCreditorValue());//赎回债权价值
			cdddto.setThisCreditorValue(tcMmCreditDetailDTO.getThisMakeCreditorValue());//赎回日期的债权价值
			cdddto.setRedeemDate(tcMmCreditDetailDTO.getRedeemDate());//赎回日期
			cdddto.setDealSerial(1L);// 交易序号
			Map<String, Object> cdddtoparamMap = new HashMap<String, Object>();
			cdddtoparamMap.put("dto", cdddto);
			// 保存债权交易流水明细表
			creditDealDetailDao.insertTCMmCreditDealDetail(cdddtoparamMap);
		}
		TCMmCooperorgDealDTO cooperorgDealDTO = new TCMmCooperorgDealDTO();
		cooperorgDealDTO.setDealCode(dealCode);//交易流水号
		cooperorgDealDTO.setCooperativeAgencyDealStatus(CimsConstant.DEAL_STATUS_CREATE);//新建
		cooperorgDealDTO.setDealType(CimsConstant.DEAL_TYPE_CREDIT_REDEEM);
		cooperorgDealDTO.setDealDate(new Date());
		cooperorgDealDTO.setRedemptionDate(creditDetailList.get(0).getRedeemDate());//赎回日期
		cooperorgDealDTO.setCreditPackageName(creditDetailList.get(0).getPackageNum());
		cooperorgDealDTO.setContractCode(creditDetailList.get(0).getContractCode());//合约编号
		cooperorgDealDTO.setCooperorgCode(creditDetailList.get(0).getCooperorgCode());//机构编号
		cooperorgDealDTO.setAgreementCode(creditDetailList.get(0).getAgreementCode());//协议
		cooperorgDealDTO.setCreateTime(new Timestamp(System.currentTimeMillis()));// 创建时间
		cooperorgDealDTO.setModifyTime(new Timestamp(System.currentTimeMillis()));// 修改时间
     	Long dealId = cooperorgDealService.insertTCMmCooperorgDeal(cooperorgDealDTO);
     	cooperorgDealDTO.setId(dealId);
		
		try {
			/**
			 * 更新保证金到交易流水明细表
			 */
			crdao.updateCreditDealDetailCautionMoney(SearchParamUtil.getSearchParam(cooperorgDealDTO));
		} catch (Exception e) {
			e.printStackTrace();
		}
     	return dealCode;
	}
	/**
	 * 获取赎回编号
	 * @author zhangmi
	 * @date  2017年1月10日
	 * @return
	 * @throws Exception
	 */
	public String doGetDealCode() throws Exception {
		return iCDeal.createDealCode(CimsConstant.BIZNO_TYPE_REDEMPTION);
	}

	/**
	 * 债权明细数据入库
	 * @author zhangmi
	 * @date  2017年2月27日
	 * @param list
	 * @param dealdto
	 */
	@Override
	public void saveCreditDetailBySecond(List<CreditDetailSecondDto> list,DealDto dealdto) {
		
		Map<String, Object> insertMap = null;
		TCMmCreditDetailDTO insertDto = null;
		TCMmCreditPackageDTO creditPackage = new TCMmCreditPackageDTO();
		creditPackage.setCooperorgCode(dealdto.getCooperorgCode());
		creditPackage.setContractCode(dealdto.getContractCode());
		creditPackage.setCreditPackageName(dealdto.getCreditPackageName());
		List<TCMmCreditPackageDTO> creditPackageList = creditPackageDao.searchTCMmCreditPackage(SearchParamUtil.getSearchParam(creditPackage));
		
		for (CreditDetailSecondDto creditDetailSecondDto : list) {
			insertMap = new HashMap<String, Object>();
			insertDto = new TCMmCreditDetailDTO();
			CreditDetailSecondDto.ToCreditDetailDto(creditDetailSecondDto, insertDto);
			if(CollectionsUtils.isNotEmpty(creditPackageList)){
				insertDto.setPlanRedeemDate(creditPackageList.get(0).getPlanRedeemDate());
			}
			insertDto.setId(null);
			insertDto.setCreditSource(CimsConstant.CREDIT_SOURCE_CRE);// 债权来源:01-债权池
			insertDto.setCreditorStatus(CimsConstant.CREDIT_STATUS_WAY);// 债权状态:01-在途
			insertDto.setPackageNum(dealdto.getCreditPackageName());
			insertDto.setContractCode(dealdto.getContractCode());
			insertDto.setCreateTime(new Timestamp(new Date().getTime()));
			insertDto.setCreditorValueUpdateDate(new java.util.Date());
			
			insertMap.put("dto", insertDto);
			creditDetailDao.insertTCMmCreditDetail(insertMap);
		}
	}
	
	/**
	 * 保存债权交易明细
	 * @author zhangmi
	 * @date  2017年2月27日
	 * @param list
	 * @param dealdto
	 */
	@Override
	public void saveCreditDealDetailBySecond(List<CreditDetailSecondDto> list,DealDto dealdto){

		Map<String, Object> insertMap = null;
		TCMmCreditDealDetailDTO insertDto = null;
		for (CreditDetailSecondDto creditDetailSecondDto : list) {
			insertMap = new HashMap<String, Object>();
			insertDto = new TCMmCreditDealDetailDTO();
			//copy字段
			CreditDetailSecondDto.ToCreditDealDetailDto(creditDetailSecondDto, insertDto);

			insertDto.setDealStreamCode(dealdto.getDealCode());//交易流水号
			insertDto.setRepaymentAffiliation(dealdto.getRepaymentAffiliation());//月换归属
			insertDto.setCostState(CimsConstant.COST_STATUS_WJ);//未结算
			insertDto.setCreateTime(new Timestamp(System.currentTimeMillis()));// 创建时间
			insertDto.setModifyTime(new Timestamp(System.currentTimeMillis()));// 修改时间
			dealdto.getCreditReferDate();
			insertMap.put("dto", insertDto);
			creditDealDetailDao.insertTCMmCreditDealDetail(insertMap);
		}
		
	}
	
	/**
	 * 插入额度插入
	 * @author zhangmi
	 * @date  2017年2月27日
	 * @param list
	 * @param dealdto
	 */
	@Override
	public void saveQuotaCreditDetailBySecond(List<CreditDetailSecondDto> list,DealDto dealdto){
		
		Map<String, Object> searchParams = null;
		Map<String, Object> insertMap = null;
		TCMmQuotaCreditDetailDTO insertDto = null;
		QuotaCreditDetailDto searchDto = null;
		for (CreditDetailSecondDto creditDetailSecondDto : list) {
			//初始对象
			insertMap = new HashMap<String, Object>();
			insertDto = new TCMmQuotaCreditDetailDTO();
			searchParams = new HashMap<String, Object>();
			searchDto = new QuotaCreditDetailDto();
			
			searchDto.setTransferId(creditDetailSecondDto.getTransferId());//债权转让编号
			searchDto.setQuotaCode(creditDetailSecondDto.getApplyId());//额度编号
			searchDto.setDealStatus(CimsConstant.QUOTA_DETAIL_STATUS_NEW);//申请
			searchParams.put("dto", searchDto);
			List<QuotaCreditDetailDto> quotaList = quotaCreditDetailDao.searchQuotaCreditDetail(searchParams);
			//如果额度明细存在
			if(CollectionsUtils.isNotEmpty(quotaList)){
				//把额度修改为占用
				for (QuotaCreditDetailDto quotaCreditDetailDto : quotaList) {
					quotaCreditDetailDto.setDealStatus(CimsConstant.QUOTA_DETAIL_STATUS_LOCK);
					quotaCreditDetailDao.updateQuotaCreditDetailStatusById(SearchParamUtil.getSearchParam(quotaCreditDetailDto));
				}
			}else{//否则：不存在:插入
				CreditDetailSecondDto.ToQuotaCreditDetailDto(creditDetailSecondDto, insertDto);
				insertDto.setDealStatus(CimsConstant.QUOTA_DETAIL_STATUS_LOCK);
				dealdto.getCreditReferDate();
				insertMap.put("dto", insertDto);
				tcmmQuotaCreditDetailDao.insertTCMmQuotaCreditDetail(insertMap);
			}
		}
	}
	
}